package com.weavernorth.salary;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 此action是终端店铺任务下达流程（月度）与年度制定的对比,给表单字段赋值
 */
public class SaleTaskControlAction extends BaseBean implements Action {

    public static Log log = LogFactory.getLog(SaleTaskControlAction.class.getName());

    @Override
    public String execute(RequestInfo request) {

        //数据操作
        RecordSet rs = new RecordSet();
        RecordSet rsSelect = new RecordSet();
        RecordSet rsUpdate = new RecordSet();
        RecordSet rsMain = new RecordSet();
        //当前表
        String strTable = request.getRequestManager().getBillTableName();
        //当前请求id
        String strRequestid = Util.null2String(request.getRequestid());
        //控制 节点控制字段
        boolean flag = true;
        //查询明细表所有数据
        String strSql = "select * from " + strTable + " fm," + strTable + "_dt1 fmd where fm.id=fmd.mainid and fm.requestid=?";
        log.info("查询表数据:" + strSql + ",requestid:" + strRequestid);
        rs.executeQuery(strSql, strRequestid);
        while (rs.next()) {
            //店铺编号
            String strShopId = Util.null2String(rs.getString("dpbh"));
            //月份
            String strMonth = Util.null2String(rs.getString("yf"));
            //年份
            String strYear = Util.null2String(rs.getString("nf"));
            //销售任务额
            Double douXsrwe = Util.getDoubleValue(rs.getString("xsrwe"), 0.0);
            //分公司
            String strFgs = Util.null2String(rs.getString("fgs"));
            //根据月份得到查询的字段
            String strField = getField(strMonth);
            //主表id
            String strMainid = "";
            //查询当前销售任务额 是否超过年度指定的销售任务额的10%
            //查询当前主表的数据
            String strMainSql = "select id from uf_rwxdbnd  where fgs=? and nf=? order by xgrq desc";
            rsMain.executeQuery(strMainSql, strFgs, strYear);
            if (rsMain.next()) {
                strMainid = Util.null2String(rsMain.getString("id"));
                String sql = "select 1 from uf_rwxdbnd_dt1 where mainid=? and dpbh=? and (" + strField + " *1.1<? or "+strField+"*0.9>?)";
                rsSelect.executeQuery(sql, strMainid, strShopId, douXsrwe,douXsrwe);
                log.info("查询销售任务额是否超过10%" + sql + ",strShopId:" + strShopId + ",strMainid:" + strMainid + ",douXsrwe:" + douXsrwe);
                //如果超过,则更新表单字段
                if (rsSelect.next()) {
                    String strUpSql = "update " + strTable + " set jdkz='0' where requestid=?";
                    log.info("更新表单字段:" + strUpSql);
                    rsUpdate.executeUpdate(strUpSql, strRequestid);
                    flag = false;
                    //跳出while循环
                    break;
                }
            }
        }

        //如果没有超过10%的,则更新 节点控制字段为 否
        if (flag) {
            String strUpSql = "update " + strTable + " set jdkz='1' where requestid=?";
            log.info("更新表单字段:" + strUpSql);
            rsUpdate.executeUpdate(strUpSql, strRequestid);
        }
        return SUCCESS;
    }

    /**
     * 根据选择的月份,得到查询 销售任务汇总表（年度）的字段
     */
    public String getField(String strMonth) {
        //返回字段是
        String strField = "";
        //一月
        if ("0".equals(strMonth)) {
            strField = "yyxse";
        }
        //二月
        if ("1".equals(strMonth)) {
            strField = "eyxse";
        }
        //三月
        if ("2".equals(strMonth)) {
            strField = "syxse";
        }
        //四月
        if ("3".equals(strMonth)) {
            strField = "syxse1";
        }
        //五月
        if ("4".equals(strMonth)) {
            strField = "wyxse";
        }
        if ("5".equals(strMonth)) {
            strField = "lyxse";
        }
        if ("6".equals(strMonth)) {
            strField = "qyxse";
        }
        if ("7".equals(strMonth)) {
            strField = "byxse";
        }
        if ("8".equals(strMonth)) {
            strField = "jyxse";
        }
        if ("9".equals(strMonth)) {
            strField = "syxse2";
        }
        if ("10".equals(strMonth)) {
            strField = "syyxse";
        }
        if ("11".equals(strMonth)) {
            strField = "seyxse";
        }

        return strField;

    }
}
